package gui;

import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import controller.BtnClearFieldsAction;
import controller.BtnCloseAction;
import controller.RegisterWindowEvent;

public class RegisterUI extends JFrame {

	private static final long serialVersionUID = 0xA1L;

	private JButton btn_clearfields;
	private JButton btn_close;
	private JButton btn_register;
	private JLabel lbl_account;
	private JLabel lbl_confirm;
	private JLabel lbl_country;
	private JLabel lbl_full_name;
	private JLabel lbl_password;
	private JLabel lbl_phone;
	private JPanel pn_account_info;
	private JPanel pn_user_info;
	private JTextField tf_account_id;
	private JPasswordField tf_confirm;
	private JTextField tf_country;
	private JTextField tf_full_name;
	private JPasswordField tf_password;
	private JTextField tf_phone;

	private static RegisterUI instance;

	public static final RegisterUI getInstance() {
		if (instance == null)
			return instance = new RegisterUI();
		return instance;
	}

	private RegisterUI() {
		initComponents();
		events();
	}

	private void events() {
		this.addWindowListener(new RegisterWindowEvent(this));
		this.btn_close.addActionListener(new BtnCloseAction(this, IndexUI
				.getInsntace()));
		
		this.btn_clearfields.addActionListener(new BtnClearFieldsAction(this));
	}

	private void newComps() {
		pn_account_info = new JPanel();
		lbl_account = new JLabel();
		lbl_password = new JLabel();
		tf_account_id = new JTextField();
		tf_password = new JPasswordField();
		lbl_confirm = new JLabel();
		tf_confirm = new JPasswordField();
		pn_user_info = new JPanel();
		lbl_full_name = new JLabel();
		tf_full_name = new JTextField();
		lbl_country = new JLabel();
		tf_country = new JTextField();
		lbl_phone = new JLabel();
		tf_phone = new JTextField();
		btn_register = new JButton();
		btn_close = new JButton();
		btn_clearfields = new JButton();
	}

	private void setCompsProperties() {
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("CA Team Register");
		setIconImage(new ImageIcon("src/icons/soft_icon_32x32.png").getImage());
		setResizable(false);

		pn_account_info.setBorder(BorderFactory
				.createTitledBorder("Register Information"));
		lbl_account.setText("Account");
		lbl_password.setText("Password");
		lbl_confirm.setText("Confirm");

		pn_user_info.setBorder(BorderFactory
				.createTitledBorder("User information"));
		lbl_full_name.setText("Full Name");
		lbl_country.setText("Country");
		lbl_phone.setText("Phone No");

		btn_register.setText("Register");
		btn_close.setText("Close");
		btn_clearfields.setText("Clear fields");
	}

	private void pn_account_infoLayout() {
		GroupLayout pn_account_infoLayout = new GroupLayout(pn_account_info);
		pn_account_info.setLayout(pn_account_infoLayout);
		pn_account_infoLayout
				.setHorizontalGroup(pn_account_infoLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pn_account_infoLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pn_account_infoLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addGroup(
																pn_account_infoLayout
																		.createSequentialGroup()
																		.addComponent(
																				lbl_confirm)
																		.addGap(26,
																				26,
																				26)
																		.addComponent(
																				tf_confirm))
														.addGroup(
																pn_account_infoLayout
																		.createSequentialGroup()
																		.addGroup(
																				pn_account_infoLayout
																						.createParallelGroup(
																								GroupLayout.Alignment.LEADING)
																						.addComponent(
																								lbl_password)
																						.addComponent(
																								lbl_account))
																		.addPreferredGap(
																				LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				pn_account_infoLayout
																						.createParallelGroup(
																								GroupLayout.Alignment.LEADING)
																						.addComponent(
																								tf_account_id)
																						.addComponent(
																								tf_password))))
										.addContainerGap()));
		pn_account_infoLayout
				.setVerticalGroup(pn_account_infoLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pn_account_infoLayout
										.createSequentialGroup()
										.addGap(25, 25, 25)
										.addGroup(
												pn_account_infoLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_account)
														.addComponent(
																tf_account_id,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pn_account_infoLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_password)
														.addComponent(
																tf_password,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pn_account_infoLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_confirm)
														.addComponent(
																tf_confirm,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(23, Short.MAX_VALUE)));

	}

	private void pn_user_infoLayout() {
		GroupLayout pn_user_infoLayout = new GroupLayout(pn_user_info);
		pn_user_info.setLayout(pn_user_infoLayout);
		pn_user_infoLayout
				.setHorizontalGroup(pn_user_infoLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pn_user_infoLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pn_user_infoLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addGroup(
																pn_user_infoLayout
																		.createSequentialGroup()
																		.addGroup(
																				pn_user_infoLayout
																						.createParallelGroup(
																								GroupLayout.Alignment.LEADING)
																						.addComponent(
																								lbl_country)
																						.addComponent(
																								lbl_full_name))
																		.addPreferredGap(
																				LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				pn_user_infoLayout
																						.createParallelGroup(
																								GroupLayout.Alignment.LEADING)
																						.addComponent(
																								tf_full_name)
																						.addComponent(
																								tf_country,
																								GroupLayout.DEFAULT_SIZE,
																								262,
																								Short.MAX_VALUE)))
														.addGroup(
																GroupLayout.Alignment.TRAILING,
																pn_user_infoLayout
																		.createSequentialGroup()
																		.addComponent(
																				lbl_phone)
																		.addPreferredGap(
																				LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				tf_phone)))
										.addContainerGap()));
		pn_user_infoLayout
				.setVerticalGroup(pn_user_infoLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pn_user_infoLayout
										.createSequentialGroup()
										.addGap(25, 25, 25)
										.addGroup(
												pn_user_infoLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_full_name)
														.addComponent(
																tf_full_name,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pn_user_infoLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_country)
														.addComponent(
																tf_country,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pn_user_infoLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(lbl_phone)
														.addComponent(
																tf_phone,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(20, Short.MAX_VALUE)));
	}

	private void setMainLayout() {
		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.LEADING)
												.addComponent(
														pn_user_info,
														GroupLayout.Alignment.TRAILING,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														pn_account_info,
														GroupLayout.Alignment.TRAILING,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		btn_register)
																.addPreferredGap(
																		LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		btn_clearfields)
																.addPreferredGap(
																		LayoutStyle.ComponentPlacement.RELATED,
																		GroupLayout.DEFAULT_SIZE,
																		Short.MAX_VALUE)
																.addComponent(
																		btn_close,
																		GroupLayout.PREFERRED_SIZE,
																		121,
																		GroupLayout.PREFERRED_SIZE)))
								.addContainerGap()));

		layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {
				btn_clearfields, btn_close, btn_register });

		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(pn_account_info,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(pn_user_info,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.BASELINE)
												.addComponent(btn_register)
												.addComponent(btn_close)
												.addComponent(btn_clearfields))
								.addContainerGap(GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
		this.setLocationRelativeTo(null);
	}

	private void initComponents() {
		newComps();
		setCompsProperties();
		pn_account_infoLayout();
		pn_user_infoLayout();
		setMainLayout();
	}

	public JButton getBtn_clearfields() {
		return btn_clearfields;
	}

	public void setBtn_clearfields(JButton btn_clearfields) {
		this.btn_clearfields = btn_clearfields;
	}

	public JButton getBtn_close() {
		return btn_close;
	}

	public void setBtn_close(JButton btn_close) {
		this.btn_close = btn_close;
	}

	public JButton getBtn_register() {
		return btn_register;
	}

	public void setBtn_register(JButton btn_register) {
		this.btn_register = btn_register;
	}

	public JLabel getLbl_account() {
		return lbl_account;
	}

	public void setLbl_account(JLabel lbl_account) {
		this.lbl_account = lbl_account;
	}

	public JLabel getLbl_confirm() {
		return lbl_confirm;
	}

	public void setLbl_confirm(JLabel lbl_confirm) {
		this.lbl_confirm = lbl_confirm;
	}

	public JLabel getLbl_country() {
		return lbl_country;
	}

	public void setLbl_country(JLabel lbl_country) {
		this.lbl_country = lbl_country;
	}

	public JLabel getLbl_full_name() {
		return lbl_full_name;
	}

	public void setLbl_full_name(JLabel lbl_full_name) {
		this.lbl_full_name = lbl_full_name;
	}

	public JLabel getLbl_password() {
		return lbl_password;
	}

	public void setLbl_password(JLabel lbl_password) {
		this.lbl_password = lbl_password;
	}

	public JLabel getLbl_phone() {
		return lbl_phone;
	}

	public void setLbl_phone(JLabel lbl_phone) {
		this.lbl_phone = lbl_phone;
	}

	public JPanel getPn_account_info() {
		return pn_account_info;
	}

	public void setPn_account_info(JPanel pn_account_info) {
		this.pn_account_info = pn_account_info;
	}

	public JPanel getPn_user_info() {
		return pn_user_info;
	}

	public void setPn_user_info(JPanel pn_user_info) {
		this.pn_user_info = pn_user_info;
	}

	public JTextField getTf_account_id() {
		return tf_account_id;
	}

	public void setTf_account_id(JTextField tf_account_id) {
		this.tf_account_id = tf_account_id;
	}

	public JPasswordField getTf_confirm() {
		return tf_confirm;
	}

	public void setTf_confirm(JPasswordField tf_confirm) {
		this.tf_confirm = tf_confirm;
	}

	public JTextField getTf_country() {
		return tf_country;
	}

	public void setTf_country(JTextField tf_country) {
		this.tf_country = tf_country;
	}

	public JTextField getTf_full_name() {
		return tf_full_name;
	}

	public void setTf_full_name(JTextField tf_full_name) {
		this.tf_full_name = tf_full_name;
	}

	public JPasswordField getTf_password() {
		return tf_password;
	}

	public void setTf_password(JPasswordField tf_password) {
		this.tf_password = tf_password;
	}

	public JTextField getTf_phone() {
		return tf_phone;
	}

	public void setTf_phone(JTextField tf_phone) {
		this.tf_phone = tf_phone;
	}

}
